/*	HTML5AS3MultiUp v0.1 <http://code.google.com/p/html5-as3-multiupload/> 
	is released under the MIT License <http://www.opensource.org/licenses/mit-license.php> 
*/

var HTML5AS3MultiUp = (function(window){
	var Const, 
		thisObj,
		_setting,
		_h5up = null,
		_jbrowser = HTML5MUP.browser,
		_renderHTML5 = function(){
			for (var k in _setting)
				if (k.indexOf('callback_')>-1)
					_setting[k] = eval(_setting[k]);
			
			_h5up = new HTML5MUP(_setting);
		},
		_renderSWF = function(){
			var realCtn = document.createElement('div');
			realCtn.id = _setting.container_id + 'Inner';
			document.getElementById(_setting.container_id).innerHTML='';
			document.getElementById(_setting.container_id).appendChild(realCtn);
			
			var flashvars = {};
			
			for (var k in _setting)
				flashvars[k] = _setting[k];
			
			var params = _setting.swf_params || {
				menu: "false",
				scale: "noScale",
				allowFullscreen: "true",
				allowScriptAccess: "always",
				bgcolor: "#fff",
				wmode: "direct" 
			};
			var attributes = _setting.swf_attributes || {
				id:_setting.swf_id,
				name:_setting.swf_id
			};
			swfobject.embedSWF(
				_setting.swf_path, 
				realCtn.id, _setting.btn_select_width, _setting.btn_select_height, "10.0.0", 
				_setting.swf_expresspath||"expressInstall.swf", 
				flashvars, params, attributes);
		},
		_bindSubmit = function(){
			document.getElementById(_setting.submit_id).onclick = function(){
				try{
					_getSWF(_setting.swf_id).clickSubmit();
				}catch(ex){}
			}
		},
		_mouseSubmit = function(){
			//todo hover样式
		},
		_getSWF = function(movieName) {
			return (navigator.appName.indexOf("Microsoft") != -1) ? window[movieName] : document[movieName];
		},
		_html5Enabled = HTML5MUP.html5Enabled,
		_dndEnabled = HTML5MUP.dndEnabled;
	
	Const = function(setting){
		_setting = setting;
		thisObj = this;
	};
	Const.prototype = {
		render: function(){
			if (_html5Enabled()){	//html5
				_renderHTML5();
				
				_mouseSubmit();
			}else{					//flash
				_renderSWF();
				
				_bindSubmit();
			}
		},
		cancelItem: function(idx){
			if (_html5Enabled()){
				_h5up.cancelItem(idx);
			}else{
				_getSWF(_setting.swf_id).cancelItem(idx);
			}
		},
		toggleButton: function(type){
			var ctn = document.getElementById('btncontainer'),
				swf = _getSWF(_setting.swf_id);
				
			ctn.className = type;
			
			if (_html5Enabled()){
				//todo
			}else{
				try{
					swf.toggleButton(type);
				}catch(ex){}
			}
			
			swf = null;
			ctn = null;
		},
		reactive: function(type){
			if (_html5Enabled()){
				_h5up.reactive();
			}else{
				try{
					_getSWF(_setting.swf_id).reactive();
				}catch(ex){}
			}
		}
	};
	Const.html5Enabled = _html5Enabled;
	Const.dndEnabled = _dndEnabled;
	Const.browser = _jbrowser;
	
	return Const;
}(window));

